package com.example.weather_forecast;


import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class CityAdapter extends RecyclerView.Adapter<CityAdapter.CityViewHolder> {

    private List<City> cities;
    private OnItemClickListener listener;

    public interface OnItemClickListener {
        void onItemClick(City city);
        void onItemLongClick(City city);
    }

    public CityAdapter(List<City> cities, OnItemClickListener listener) {
        this.cities = cities;
        this.listener = listener;
    }

    @Override
    public CityViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.city_item, parent, false);
        return new CityViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(CityViewHolder holder, int position) {
        City city = cities.get(position);
        holder.cityName.setText(city.getName());
        holder.itemView.setOnClickListener(v -> listener.onItemClick(city));
        holder.itemView.setOnLongClickListener(v -> {
            listener.onItemLongClick(city);
            return true;
        });
    }

    @Override
    public int getItemCount() {
        return cities == null ? 0 : cities.size();
    }

    public void setCities(List<City> cities) {
        this.cities = cities;
        notifyDataSetChanged();
    }

    class CityViewHolder extends RecyclerView.ViewHolder {

        TextView cityName;

        public CityViewHolder(View itemView) {
            super(itemView);
            cityName = itemView.findViewById(R.id.cityName);
        }
    }
}

